package logger;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import models.Account;
import models.Userlog;

public class LogSchedular {
	private static ExecutorService pool = null;

	static public void logLogin(Account user) {
		if (pool != null) {
			Userlog ul = new Userlog();
			ul.setAccount(user);
			ul.setAction("登录");
			ul.setUpdatetime(new Date());
			pool.execute( new LogJob(ul));
		}
	}
	
	static public void logAdd(Account user, String table, int rowid) {
		if (pool != null) {
			Userlog ul = new Userlog();
			ul.setAccount(user);
			ul.setAction("新增");
			ul.setModel(table);
			ul.setRowid(rowid);
			ul.setUpdatetime(new Date());
			pool.execute( new LogJob(ul));
		}
	}
	
	static public void logDelete(Account user, String table, int rowid) {
		if (pool != null) {
			Userlog ul = new Userlog();
			ul.setAccount(user);
			ul.setAction("删除");
			ul.setModel(table);
			ul.setRowid(rowid);
			ul.setUpdatetime(new Date());
			pool.execute( new LogJob(ul));
		}
	}
	
	static public void logModify(Account user, String table, int rowid, String column) {
		if (pool != null) {
			Userlog ul = new Userlog();
			ul.setAccount(user);
			ul.setAction("修改");
			ul.setModel(table);
			ul.setRowid(rowid);
			ul.setField(column);
			ul.setUpdatetime(new Date());
			pool.execute( new LogJob(ul));
		}
	}
	
	static public void start() {
		if (pool == null) {
			pool = Executors.newSingleThreadExecutor();
		}
	}
	
	static public void stop() {
		if (pool != null) {
			pool.shutdown();
		}
	}
}
